using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BUS;
using Utility;
using Entities;
public partial class Admin_Suatour : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loaddropdownlist();
            LoadData();

        }
    }
    private void loaddropdownlist()
    {
        dr_tenlt.DataSource = tbLoaitourBUS.GetAll();
        dr_tenlt.DataTextField = tbLoaitourCollumn.tenloaitour.ToString();
        dr_tenlt.DataValueField = tbLoaitourCollumn.maloaitour.ToString();
        dr_tenlt.DataBind();
        dr_tenlt.Items.Insert(0, new ListItem("All", "0"));
    }
    private void LoadData()
    {
        try
        {
            int id = int.Parse(Request.QueryString["id"].ToString());
            txt_mat.Text = id.ToString();

            tbTour  tour = tbTourBUS .GetByID(id);
            txttent.Text = tour.Tentour ;
            dr_tenlt.SelectedValue = tour.Maloaitour .ToString();
            txt_tg.Text = tour.Thoigian;
            txt_pt.Text = tour.Phuongtien;
            txt_g.Text = tour .Gia .ToString();
            txt_kh.Text = tour.Khoihanh;

            txt_mota.Text = tour.Mota ;

            img_hmh.ImageUrl = "~/Hinhanh/Hinh/" + tour.Hinh;


        }
        catch
        {
            Response.Redirect("~/Admin/Danhsachtour.aspx");
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {

        int id = int.Parse(Request.QueryString["id"].ToString());
        tbTour  tour = tbTourBUS .GetByID(id);

        tour.Tentour  = txttent.Text;

        tour.Maloaitour  = int.Parse(dr_tenlt.SelectedValue);
        tour.Thoigian = txt_tg.Text;
        tour.Phuongtien = txt_pt.Text;
        tour .Gia = float.Parse(txt_g.Text);
        tour.Khoihanh = txt_kh.Text;
        tour.Mota  = txt_mota .Text;

        if (fu_hmh.HasFile)
        {
            string filename = DateTime.Now.Ticks.ToString() + "_" + fu_hmh.FileName;
            fu_hmh.SaveAs(Server.MapPath("~/Hinhanh/" + filename));

            System.Drawing.Image img = System.Drawing.Image.FromFile(
                Server.MapPath("~/Hinhanh/" + filename));

            ImageUtility.MakeThumbnailPhoto(
                img, 100, 100, Server.MapPath("~/Hinhanh/Hinh/"), filename);
            // xoa hinh cu
            if (tour.Hinh != "no_images.jpg")
            {
                System.IO.File.Delete(Server.MapPath("~/Hinhanh/" + tour.Hinh));
                System.IO.File.Delete(Server.MapPath("~/Hinhanh/Hinh/" + tour.Hinh));
            }
            tour.Hinh = filename;
        }
        tbTourBUS .Update(tour);
        Response.Redirect("~/Admin/Danhsachtour.aspx");
    }
}
